//
//  ShopMenu.m
//  AirPort
//
//  Created by henrySun on 12-9-28.
//  Copyright (c) 2012年 广州民航信息技术有限公司. All rights reserved.
//


#define TOPHEIGHT 38
#import "Utility.h"
#import "BwhcTopMenu.h"
#import "ArrowView.h"
#import "Config.h"
#import "QuartzCore/QuartzCore.h"


@implementation BwhcTopMenu

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor=[UIColor colorWithPatternImage:[Utility imageNamed:@"bwhc_menubg.png"]];
        [self shopMenu];
    }
    return self;
}
-(void) shopMenu{
    self.itemArray=[NSMutableArray array];
    UIView *newsMenu=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, TOPHEIGHT)];
    newsMenu.backgroundColor=[UIColor clearColor];
    ArrowView *leftButton=[[ArrowView alloc] initWithFrame:CGRectMake(6, 14,8,10)];
    leftButton.image=[Utility imageNamed:@"bwhc_arrowleft.png"];
    leftButton.tag=HOMEPAGESECONDLEFTBUTTONTAG;
    leftButton.hidden=YES;
    leftButton.delegate=self;
    [newsMenu  addSubview:leftButton];
	leftButton.direction = kLeft;
    ArrowView *rightButton=[[ArrowView alloc] initWithFrame:CGRectMake(306, 14,8,10)];
    rightButton.image=[Utility imageNamed:@"bwhc_arrowright.png"];
    rightButton.tag=HOMEPAGESECONDRIGHTBUTTONTAG;
    rightButton.delegate=self;
    rightButton.hidden=YES;
    rightButton.direction = kRight;
    [newsMenu  addSubview:rightButton];
    
    
    newsMenu.backgroundColor=[UIColor  clearColor];
    UIScrollView *scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(20, 0, 280, TOPHEIGHT)];
    self.menuScrollView=scrollView;
    self.menuScrollView.delegate=self;
    self.menuScrollView.showsHorizontalScrollIndicator=NO;
    [self.menuScrollView setBackgroundColor:[UIColor  clearColor]];
    [newsMenu addSubview:self.menuScrollView];
    [self addSubview:newsMenu];
    
    
}
-(void) showData:(NSMutableArray *) param andPadding:(float)padding{
    [self.itemArray removeAllObjects];
    [self.itemArray addObjectsFromArray:param];
    for (UIView  *view in self.menuScrollView.subviews){
        [view removeFromSuperview];
    }
    if (param&&[param  count]>0) {
        for (UIView  *view  in self.menuScrollView.subviews) {
            for (UIView  *subview  in view.subviews) {
                [subview removeFromSuperview];
            }
            [view removeFromSuperview];
        }
     	[self.menuScrollView setCanCancelContentTouches:NO];
        self.menuScrollView.scrollEnabled=YES;
        self.menuScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
        float xPosition = 0;
        //第一按钮 默认显示
        for ( int i=0;i<self.itemArray.count;i++) {
            NSDictionary *dict=[self.itemArray objectAtIndex:i];
            UIFont *font=[UIFont fontWithName:FONT_FAMILY size:15.0f];
            CGSize size=[Utility getStringSize:[dict objectForKey:@"name"]font:font withMaxSize:CGSizeMake(300, 20)];
            CGRect frame=CGRectMake(xPosition, 0, size.width+padding*2, TOPHEIGHT);
            UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
            button.frame=frame;
            [button setTitle:[dict objectForKey:@"name"] forState:UIControlStateNormal];
            [button setTitleColor:UIColorFromRGB(0x666666) forState:UIControlStateNormal];
            [button setBackgroundColor:[UIColor clearColor]];
            button.titleLabel.font=font;
            button.layer.masksToBounds=YES;
            button.layer.cornerRadius=3.0;
            button.tag=HOMEPAGESECONDCENTERBUTTONTAG+i;
            [button addTarget:self action:@selector(thumbButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            [self.menuScrollView addSubview:button];
            xPosition += frame.size.width + 6;
            if (i==0) {
                [self thumbButtonAction:button];
            }
        }
        [self.menuScrollView setContentSize:CGSizeMake(xPosition, self.menuScrollView.frame.size.height)];
        if (self.menuScrollView.frame.size.width<self.menuScrollView.contentSize.width) {
            ArrowView *rightButton=(ArrowView *)[self  viewWithTag:HOMEPAGESECONDRIGHTBUTTONTAG];
            rightButton.hidden=NO;
        }
        
    }
}

-(IBAction)thumbButtonAction:(id)sender{
    UIButton *button=(UIButton*)sender;
    for (int i=0;i<self.itemArray.count;i++) {
        UIButton *buttontemp=(UIButton*)([self viewWithTag:HOMEPAGESECONDCENTERBUTTONTAG+i]);
        buttontemp.selected=NO;
        [buttontemp setBackgroundColor:[UIColor clearColor]];
        [buttontemp setTitleColor:UIColorFromRGB(0x666666) forState:UIControlStateNormal];
    }
     button.selected=NO;
     button.backgroundColor=UIColorFromRGB(0x3aaf0c);
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    if ([self.delegate respondsToSelector:@selector(onclickScrollButton:)]) {
        [self.delegate  onclickScrollButton:button.tag-HOMEPAGESECONDCENTERBUTTONTAG];
       
    }
}

#pragma mark ArrowViewDelegate
- (BOOL) scroll:(ArrowView *)arrowView withDirection:(NSInteger)direction {
    ArrowView *leftButton=(ArrowView *)[self  viewWithTag:HOMEPAGESECONDLEFTBUTTONTAG];
    ArrowView *rightButton=(ArrowView *)[self  viewWithTag:HOMEPAGESECONDRIGHTBUTTONTAG];
	CGPoint offset = self.menuScrollView.contentOffset;
	float maxX = self.menuScrollView.contentSize.width - self.menuScrollView.bounds.size.width;
	if (direction == kLeft) {
		offset.x -= 4;
	}
	else {
		offset.x += 4;
	}
	if (offset.x < 0) {
		offset.x = 0;
	}
	else if (offset.x > maxX) {
		offset.x = maxX;
	}
	self.menuScrollView.contentOffset = offset;
	if (offset.x == 0 || offset.x == maxX) {
		return YES;
	}
	else {
        leftButton.hidden = NO;
        rightButton.hidden = NO;
		return NO;
	}
}

#pragma UIscrollDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    ArrowView *leftButton=(ArrowView *)[self  viewWithTag:HOMEPAGESECONDLEFTBUTTONTAG];
    leftButton.hidden=NO;
    ArrowView *rghtButton=(ArrowView *)[self  viewWithTag:HOMEPAGESECONDRIGHTBUTTONTAG];
    rghtButton.hidden=NO;
    
    if (self.menuScrollView.contentOffset.x<1) {
        leftButton.hidden=YES;
    }
    if (self.menuScrollView.contentOffset.x>self.menuScrollView.contentSize.width-self.menuScrollView.frame.size.width-1) {
        rghtButton.hidden=YES;
    }
    
}
-(void) dealloc{
    _delegate=nil;
    self.menuScrollView.delegate=nil;
}

@end
